<?php
/**
 * Created by PhpStorm.
 * User: chendan
 * Date: 2017/2/26
 * Time: 下午1:20
 */

function xrange($start, $end, $step = 1)
{
    for ($i = $start; $i <= $end; $i += $step) {
        yield $i;
    }
}


function logger($fileName)
{
    $fileHandle = fopen($fileName, 'a');
    while (true) {
        fwrite($fileHandle, yield . "\n");
    }
}

//$range = xrange(1, 1000000);
//var_dump($range); // object(Generator)#1
//var_dump($range instanceof Iterator); // bool(true)

//$fileInfo = new SplFileInfo();
$startTime = microtime(true);
echo $startTime;
echo "\n";

$logger = logger(__DIR__ . '/number');
foreach (xrange(1, 1000000000) as $num) {
    $logger->send($num);
    if ($num % 500 === 0) {
        echo sprintf('%6d: ', $num), memory_get_usage(), " bytes\n";
    }

//    echo $num, "\n";
//    exit();
}

$endTime = microtime(true);
echo $endTime;
echo "\n";
echo $endTime - $startTime;